import { http } from "@/utils/http";
import { baseAdminURLApi, type PageResult } from "../utils";

export type ArticleVO = {
  id: number;
  articleTitle: string;
  categoryName: string;
  summary: string;
  tagNameList: Array<string>;
  cover: string;
  isTop: number;
  status: number;
};
export type ArticleQuery = {
  currentPage: number;
  pageSize: number;
  articleTitle: string;
  tagId?: number;
  categoryId?: number;
  isTop?: number;
  status?: number;
};
export type ArticleDTO = {
  title: string;
  content: string;
  summary: string;
  cover: string;
  categoryId: number;
  tags: Array<string>;
  isTop: number;
  status: number;
};

export interface ArticleForm {
  id: string;
  title: string;
  author: string;
  content: string;
  summary: string;
  cover: string;
  categoryId: number | null;
  tags: Array<string>; // 标签 id
  isTop: 1 | 2; // 1 = 置顶, 2 = 取消置顶
  order: number;
  status: 1 | 2; // 1 = 公开, 2 = 私密
  coverList: Array<any>;
  tagIdList: Array<any>;
}

/** 条件分页获取文章 */
export const getArticleList = (data?: ArticleQuery) => {
  return http.request<PageResult<ArticleVO>>(
    "get",
    baseAdminURLApi("/article"),
    {
      params: data
    }
  );
};

/** 新增文章 */
export const addArticle = (data?: object) => {
  return http.request<boolean>("post", baseAdminURLApi("/article"), {
    data
  });
};

/** 修改文章 */
export const editArticle = (id?: number, data?: object) => {
  return http.request<boolean>("put", baseAdminURLApi(`/article/${id}`), {
    data
  });
};

/** 删除文章  传文章状态 1、2会假删除 3会真删除*/
export const deleteArticle = (ids: Array<number>) => {
  return http.request<boolean>("delete", baseAdminURLApi("/article"), {
    data: ids
  });
};

/** 修改文章置顶 */
export const updateArticleTop = (id, type, value) => {
  return http.request<boolean>(
    "put",
    baseAdminURLApi(`/article/${id}/${type}/${value}`)
  );
};

/** 恢复文章 */
export const revertArticle = id => {
  return http.request<boolean>("put", `/api/revert/${id}`);
};

/** 公开或隐藏文章 1 公开 2 私密 */
export const isArticlePublic = (id, status) => {
  return http.request<boolean>("put", `/api/article/update/${id}/${status}`);
};

/** 根据文章id获取文章详细信息 */
export const getArticleById = id => {
  return http.request<ArticleDTO>("get", baseAdminURLApi(`/article/${id}`));
};

/** 根据文章标题 和 id 判断文章标题是否重复了 */
export const titleExist = data => {
  return http.request<boolean>("get", baseAdminURLApi(`/article/check`), {
    params: data
  });
};
